import http from './http'
import { store } from '../store'
import Lyric from '../../node_modules/lyric-parser/dist/lyric';
export default {
  //获取主页个人推荐轮播图
  getBanners(params){
   return http.get('/banner',{params})
     .then((res)=>{
       store.commit('getBaner',res.data.banners);
     })
     .catch((err)=>{
       console.log(err);
     })
  },
  //获取用户歌单
  getUserMusicList(params){
    return http.get('/user/playlist',{params})
      .then((res)=>{

        store.commit('getUserLove',res.data.playlist);
        })
      .catch((err)=>{
        console.log(err);
      })
  },
  //获取用户播放记录
  getOldPlay(params){
    return http.get('/user/record',{params})
      .then((res)=>{
      store.commit('getOldPlay',res.data.weekData.length);
      })
      .catch((err)=>{
        console.log(err);
      })
  },
  //获取用户电台
  getUserDtai(params){
    return http.get('/user/audio',{params})
      .then((res)=>{

        store.commit('getUserDtai',res.data.count);
      })
      .catch((err)=>{
        console.log(err);
      })
  },
  //获取用户关注
  getUserGzhu(params){
    return http.get('/user/follows',{params})
      .then((res)=>{
        const arr = [];
        for(let i = 0; i < res.data.follow.length; i ++){
          arr.push(res.data.follow);
        }
        store.commit('getUserGzhu',res.data.follow.length);
        store.commit('getUserGzhuList',arr);
        console.log(arr);
      })
      .catch((err)=>{
        console.log(err);
      })
  },
  //获取用户详情
  getUserXqing(params){
    return http.get('/user/detail',{params})
      .then((res)=>{

        store.commit('getUserXqing',res.data.profile);
        store.commit('getFoucsUserXqing',res.data);
        })
  },
  //获取用户粉丝列表
  getUserFsi(params){
  return http.get('/user/followeds',{params})
    .then((res)=>{
      store.commit('getUserFsi',res.data.followeds.length);
    })
    .catch((err)=>{
      console.log(err);
    })
},
  //获取用户动态
  getUserDongtai(params){
    return http.get('/user/event',{params})
      .then((res)=>{
        const json = [];

        for(let i = 0; i < res.data.events.length; i ++){
          json.push(JSON.parse(res.data.events[i].json));

        }
        console.log(json);
        store.commit('getUserDongtai',res.data.size);
        store.commit('getFoucsUserDongtai',json);
      })
      .catch((err)=>{

      })
  },
  //获取视频列表
  getVideoGroup(params){
    return http.get('/video/group/list',{params});

  },
  //获取视频播放地址
  getVideoUrl(params){
    return http.get('/video/group/',{params})
      .then((res)=>{
        console.log(res);
        const urlArr = [];
        const cUrl = [];
        const title = [];
        const scount = [];
        const pcount = [];
        const avatarUrl=[];
        const nikName = [];
        for(let i = 0; i < res.data.datas.length; i ++){
          urlArr.push(res.data.datas[i].data.urlInfo.url);
          cUrl.push(res.data.datas[i].data.coverUrl);
          title.push(res.data.datas[i].data.title);
          scount.push(res.data.datas[i].data.shareCount);
          pcount.push(res.data.datas[i].data.praisedCount);
          avatarUrl.push(res.data.datas[i].data.creator.avatarUrl);
          nikName.push(res.data.datas[i].data.creator.nickname);
        }
        store.commit('getVideoUrl',urlArr);
        store.commit('getCoverUrl',cUrl);
        store.commit('getTitle',title);
        store.commit('getShareCount',scount);
        store.commit('getPraisedCount',pcount);
        store.commit('getAvatarUrl',avatarUrl);
        store.commit('getNikName',nikName);
      })
  },
  //获取动态
  getUserEvent(params){
    return http.get('/user/event',{params})
      .then((res)=>{
        console.log(res);
        const json = [];
        const urlarr = [];
        const uenent = [];
        for(let i = 0; i < res.data.events.length; i ++) {
          json.push(JSON.parse(res.data.events[i].json));
          uenent.push(res.data.events[i].info);
          http.get(`/video/url?id=${json[i].video.videoId}`)
            .then((res)=>{
              for(let i = 0; i < res.data.urls.length; i ++){
                urlarr.push(res.data.urls[i].url)
              }
              })
        }

        store.commit('getUserEventId',urlarr);
        store.commit('getUserEvent',json);
        store.commit('getUevent',uenent);
      })
  },
  //获取音乐Url
  getMusicUrl(params){

    return http.get('/song/url',{params})
      .then((res)=>{
        store.commit('getMusicUrl',res.data.data[0].url);
        http.get('/lyric',{params})
          .then((res)=>{
            const arr = [] ;
            const timer = [];

            // const musciText  = res.data.lrc.lyric.split(/\n/);

            // arr.push(musciText);

            // for(var i in musciText)
            // {
            //   var str = musciText[i];
            //   var re = /^\[+[0-2][0-4]:[0-5][0-9]\.[0-9]+\]/;
            //   let pattern = /\d{2}:\d{2}\.\d{2}/g;
            //   var time = musciText[i].match(pattern);
            //   var minute = 0;
            //   var secd = 0;
            //   var ss = 0;
            //   if(time !== null){
            //     var one = time.toString().split(':');
            //     var two = one[1].toString().split('.');
            //   }
            //   minute = parseInt(one[0]);
            //   secd = parseInt(two[0]);
            //   ss = parseInt(two[1]);
            //   timer.push(minute*60*1000+secd*1000+ss);
            //
            //   arr.push(str.split(re)[1]);
            // }

            // let lyrics = new Lyric(res.data.lrc.lyric,fn1);
            // for(let i = 0; i < lyrics.lines.length ; i ++){
            //   arr.push(lyrics.lines[i].txt);
            // }
            // function fn1(obj) {
            //   console.log(obj)
            // }
            // console.log(lyrics.lines);
            arr.push(res.data.lrc.lyric);
            store.commit('getMusciGeci',res.data.lrc.lyric);
          })
      })
  },
  //获取私信
  getSiXin(params){
    return http.get('/msg/private',{params})
      .then((res) =>{

        const sixin = [];


        for(let i = 0; i < res.data.msgs.length; i ++){
            sixin.push(JSON.parse(res.data.msgs[i].lastMsg));
            }
        console.log(res.data.newMsgCount);
        store.commit('getSiXin',sixin);
        store.commit('getSiTwo',res);
      })
  },
  //获取通知
  getTz(params){
    return http.get('/msg/notices',{params})
      .then((res) =>{
        console.log(res);
      })
  },

}
